import { createI18n } from 'vue-i18n' 
import zh from './zh'
import en from './en'
const i18n = createI18n({
    legacy: false,
    locale: 'zh',
    messages: {
      zh,
      en
    }
});
async function loadLocaleMessages(locale: string) {
    try {
      // load locale messages with dynamic import
      const messages = await import(/* webpackChunkName: "locale-[request]" */ `../${locale}.ts`);
  
      // set locale and locale message
        i18n.global.setLocaleMessage(locale, messages.default);

        // 需要合并消息而不是直接替换
        // i18n.global.mergeLocaleMessage(locale, messages.default); 
  
      return Promise.resolve();
    } catch (error) {
      console.error('i18n load error', error);
      return Promise.resolve();
    }
}
export const setI18nLanguage = async (locale: any) =>{
    await loadLocaleMessages(locale);
    console.log('setI18nLanguage', locale);
    i18n.global.locale.value = locale;
    document.documentElement.lang = locale; // 设置HTML语言属性
}

export default i18n;